import SwiftUI

struct DiscoverTopCard: View {
    var item:MainPica = mainpicas[0]
    var tag: String = "hot"
    @State var isPressed: Bool = false
    
    var body: some View {
        ZStack(alignment: .topTrailing) {  // 使用 ZStack 来叠加标签和卡片内容
            VStack(alignment: .leading) {
                ZStack{
                    CachedImageView(imageURL: item.cover)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
//                        .overlay(
//                            // 轻微的渐变遮罩，增强电影感
//                            LinearGradient(gradient: Gradient(colors: [Color("BgColor1").opacity(1), Color.clear]),
//                                           startPoint: .bottom, endPoint: .top)
//                            .frame(height: .infinity)
//                            .clipped()
//                        )
                        .clipShape(RoundedRectangle(cornerRadius: 10.0))
                    
                    // 模拟光斑效果，添加电影光影感
                    Circle()
                        .fill(Color.white.opacity(0.2))
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .position(x: 250, y: 80)
                        .blur(radius: 30)
                        .opacity(0.6)
                }
                
                VStack(alignment: .leading){
                    let tagsString = item.tags.prefix(2).map { $0 }.joined(separator: ", ")
                    
                    Text("\(tagsString)")
                        .font(.system(size: 14))
                        .foregroundStyle(.white)
                        .opacity(0.7)
                    
                    Text(item.name)
                        .font(.system(size: 16))
                        .foregroundStyle(.white)
                        .lineLimit(1)
                    Text(item.desc)
                        .font(.system(size: 14))
                        .foregroundStyle(.white)
                        .opacity(0.7)
                        .lineLimit(1)
                }.frame(height:60)
            }
            .scaleEffect(isPressed ? 0.95 : 1)
            .simultaneousGesture(
                DragGesture(minimumDistance: 0)
                    .onChanged { _ in
                        withAnimation(.easeInOut(duration: 0.2)) {
                            isPressed = true
                        }
                    }
                    .onEnded { _ in
                        withAnimation(.easeInOut(duration: 0.2)) {
                            isPressed = false
                        }
                    }
            )
            
            // 左上角的标签部分
            if !tag.isEmpty {
                Text("\(tag)")
                    .font(.system(size: 12, weight: .medium))
                    .foregroundColor(.white)
                    .padding(.horizontal, 6)  // 调整水平 padding，增加左右间距
                    .padding(.vertical, 4)     // 调整垂直 padding，减少上下间距
                    .background(Color("MainColor"))
                    .cornerRadius(5)  // 调整圆角大小，值越小，圆角越小
                    .offset(x: -4, y: 4) // 调整标签位置，偏移到卡片的左上角
            }
        }
        //        .background(Color("BgColor1"))
        .cornerRadius(10)  // 添加圆角，使卡片边角平滑
    }
}

#Preview {
    DiscoverTopCard()
        .frame(width:300,height:250)
        .padding(40)
        .background(Color("BgColor1"))
    
}
